package org.example.system.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.example.system.entity.DealLog;
import org.example.system.entity.User;
import org.example.system.service.DealLogService;
import org.example.system.service.UserService;
import org.example.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


/**
 * 用户控制层
 */
@RestController
@RequestMapping("/app/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private DealLogService dealLogService;

    /**
     * 用户登录
     * @param loginUser
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody User loginUser) {
        log.info("--------AuthController | login | input:{}", JSON.toJSONString(loginUser));
        if (StrUtil.isEmpty(loginUser.getUsername()) ||
                StrUtil.isEmpty(loginUser.getPassword())){
            return R.fail("参数错误");
        }
        Object token = userService.login(loginUser);
        return R.ok(token);
    }

    /**
     * 注册用户
     * @param user
     * @return
     */
    @PostMapping("/register")
    public R add(@RequestBody User user){
        log.info("--------- UserController | add | input : {}", JSON.toJSON(user));
        int row = userService.add(user);
        if(row <= 0){
            return R.fail("添加失败");
        }
        return R.ok("添加成功");
    }

    /**
     * 修改
     * @param user
     * @return
     */
    @PutMapping("/update")
    public R update(@RequestBody User user){
        log.info("--------- UserController | update | input : {}", JSON.toJSON(user));
        if(null == user.getId() || user.getId() <= 0){
            return R.fail("参数错误:ID未传入");
        }
        int row = userService.update(user);
        if(row <= 0){
            return  R.fail("修改失败");
        }
        return R.ok("修改成功");
    }

    /**
     * 修改密码
     * @param user
     * @return
     */
    @PutMapping("/updatePassword")
    public R updatePassword(@RequestBody User user){
        log.info("--------- UserController | updatePassword | input : {}", JSON.toJSON(user));
        if(null == user.getId() || user.getId() <= 0){
            return R.fail("参数错误:ID未传入");
        }
        //根据ID查询用户，用户存在比对传入参数的旧密码与数据库中用户的密码是否相同
        User exitUser = userService.getOne(user.getId());
        int row = -1;
        if (exitUser != null && DigestUtil.bcryptCheck(user.getOldPassword(),exitUser.getPassword())){
            row = userService.update(user);
        }
        if(row <= 0){
            return R.fail("修改密码失败");
        }
        return R.ok("修改密码成功");
    }

    /**
     * 余额充值
     * @param dealLog
     * @return
     */
    @PutMapping("/recharge")
    public R recharge(@RequestBody DealLog dealLog,HttpServletRequest request){
        //根据ID查询用户，用户存在比对传入参数的旧密码与数据库中用户的密码是否相同
        User exitUser = userService.getOne(dealLog.getUserId());
        int row = -1;
        if (exitUser != null && dealLog.getMoney() >= 0){
            exitUser.setMoney(exitUser.getMoney()+dealLog.getMoney() );
            row = userService.update(exitUser);
        }
        if(row <= 0){
            return R.fail("充值余额失败");
        }
        DealLog log = new DealLog();
        log.setUsername(exitUser.getUsername());
        log.setBlock(dealLog.getBlock());
        log.setInfo(dealLog.getInfo());
        log.setMoney(dealLog.getMoney());
        dealLogService.add(log,request);
        return R.ok("充值余额成功");
    }

    /**
     * 根据id查询用户数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R getInfo(@PathVariable("id") Long id){
        log.info("--------- UserController | getInfo | input : {}", JSON.toJSON(id));
        return R.ok(userService.getOne(id));
    }

    /**
     * 根据username查询用户数据
     * @param username
     * @return
     */
    @GetMapping("/select")
    public R getInfo(String username){
        log.info("--------- UserController | getInfo | input : {}", JSON.toJSON(username));
        return R.ok(userService.getOne(username));
    }
}
